﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameProject
{
    class Bullets
    {
        public List<Bullets> bulletList;
        private Texture2D bulletSprite;
        private Vector2 bulletStartPosition;
        private Vector2 bulletPosition;
        private Vector2 mousePosition;
        private Vector2 movement;
        private Vector2 direction;
        private SpriteFont spriteFont;
        private Rectangle bulletHitBox;
        private bool ableToReload;
        private int damage;
        private float bulletSpeed;
        private string ammoToString;
        private int maxFireRate;
        private int magazineSize;
        private int ammoLeft;
        private int fireRate;
        public void Initialize()
        {
            damage = 10;
            bulletSpeed = 0.5f;
            maxFireRate = 10;
            fireRate = 0;
            magazineSize = 30;
            ableToReload = false;
            ammoLeft = magazineSize;
        }
        public void LoadContent (ContentManager Content)
        {
            bulletSprite = Content.Load<Texture2D>("bulletSprite");
            bulletList = new List<Bullets>();
            spriteFont = Content.Load<SpriteFont>("SpriteFont1");
        }
        public void Update(GameTime gameTime, Player player, Map map)
        {
            MouseState mouse = Mouse.GetState();
            //Fire Function
            if(mouse.LeftButton == ButtonState.Pressed && ammoLeft > 0)
            {
                if (fireRate == 0)
                {
                    Bullets bullet = new Bullets();
                    bulletList.Add(bullet);
                    bullet.damage = getBulletDamage();
                    bullet.bulletStartPosition = player.getPlayerMiddle();
                    bullet.mousePosition = new Vector2(mouse.X, mouse.Y);
                    bullet.bulletPosition = bullet.bulletStartPosition;
                    bullet.movement = bullet.mousePosition - bullet.bulletStartPosition;
                    bullet.direction = bullet.mousePosition - bullet.bulletPosition;
                    bullet.bulletHitBox = new Rectangle((int)bullet.bulletPosition.X, (int)bullet.bulletPosition.Y, bulletSprite.Width, bulletSprite.Height);
                    
                    if(movement != Vector2.Zero)
                    {
                        bullet.movement.Normalize(); 
                    }
                    if (bullet.direction != Vector2.Zero)
                    {
                        bullet.direction.Normalize();
                    }
                    ammoLeft--;
                    fireRate = maxFireRate;                   
                }
                else
                {
                    fireRate--;
                }
            }
            //Reload weapon
            if(ammoLeft == 0)
            {
                ableToReload = true;
            }
            
            if (Keyboard.GetState().IsKeyDown(Keys.R) && !(mouse.LeftButton == ButtonState.Pressed) && ableToReload
                && player.getPoints() >= 30)
            {
                if(ammoLeft < magazineSize)
                {
                    for (int i = 0; i < magazineSize; i++)
                    {
                        ammoLeft += 1;
                    }
                    player.reload();
                    ableToReload = false;
                }
                fireRate = 0;
            }
            //Calculates new x and y for all bullets 
            for (int i = 0; i < bulletList.Count; i++)
            {
                bulletList[i].bulletPosition += bulletList[i].direction * bulletSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds*500;
                bulletList[i].bulletHitBox = new Rectangle((int)bulletList[i].bulletPosition.X, (int)bulletList[i].bulletPosition.Y, bulletSprite.Width, bulletSprite.Height);
                if(bulletList[i].bulletPosition.X > 1010 || bulletList[i].bulletPosition.X < -10 
                    || bulletList[i].bulletPosition.Y > 810 || bulletList[i].bulletPosition.Y < -10)
                {
                    bulletList.Remove(bulletList[i]);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Q) && !(mouse.LeftButton == ButtonState.Pressed))
            {
                
                for (int i = 0; i < bulletList.Count; i++)
                {
                    bulletList.Remove(bulletList[i]);
                }
            }
            ammoToString = ammoLeft.ToString();
        }
        public void Draw(SpriteBatch spriteBatch, Player player)
        {
            for (int i = 0; i < bulletList.Count; i++)
            {
                spriteBatch.Draw(bulletSprite, bulletList[i].bulletPosition, Color.White);
            }
            spriteBatch.DrawString(spriteFont,"Ammo: " + ammoToString, new Vector2(player.getPlayerX(), player.getPlayerY() +25), Color.Black);
        }
        public int getBulletDamage()
        {
            return damage;
        }
        public Rectangle getBulletHitBox()
        {
            return this.bulletHitBox;
        }
    }
}
